import { createBasicFramework, setSceneBackground, setCamera, getScreenToWorld, fitModelToViewport } from "./basic-framework.js";
import createDragAndRotate from "./drag-and-rotate.js"
import createAxis  from "./coordinate-axis";
import createLine from './line.js'
import createPoints from "./point";
import { loadingModels } from "./v5-loading.js";
import modelEvent from "./model-event";
import createSelectEvent from "./select-event";

import * as THREE from 'three'

export function createThreeFramework(config, dom) {

    /**
     * 旋转监听事件
     */
    config.event.dragRotate = () => {
        console.log('旋转了')
        config.renderPointToLines()
    }

    createBasicFramework(config,dom);
    Object.assign(config.framework, {
        setSceneBackground: setSceneBackground.bind(config),
        setCamera: setCamera.bind(config),
        fitModelToViewport: fitModelToViewport.bind(config)
    })


    // 监听缩放变化事件
    config.framework.controls.addEventListener('change', function() {
        // 对于OrthographicCamera
        if (config.framework.camera.isOrthographicCamera && config.framework.camera.zoom !== config.framework.lastCameraZoom) {
            console.log("正交缩放")
            config.framework.lastCameraZoom = config.framework.camera.zoom;
            config.renderPointToLines()
        }
        
        // 对于PerspectiveCamera（通过相机位置变化判断）
        if (config.framework.camera.isPerspectiveCamera) {
            if (!config.framework.lastCameraPosition) {
                config.framework.lastCameraPosition = config.framework.camera.position.clone();
            }

            const distanceChanged = config.framework.lastCameraPosition.distanceTo(config.framework.camera.position) > 0.01;
            if (!distanceChanged) return
            console.log("透视缩放")
            config.framework.lastCameraPosition.copy(config.framework.camera.position);
            config.renderPointToLines()
        }
    });

    // 坐标轴
    let axisAttr = createAxis(dom)
    // 拖拽旋转
    let rotateMethods = createDragAndRotate(dom, config.event)
    // 模型添加到旋转
    rotateMethods.addRotate(config.framework.models)
    rotateMethods.addRotate(axisAttr.axisContainer);

    Object.assign(config, {
        axisAttr,
        rotateMethods,
        // 测点对象
        pointAttrMethods: createPoints(config.framework.models, []),
        // 连线对象
        lineAttrMethods: createLine(config.framework.scene),
        renderModels: async (treeJson) => {
            await loadingModels(config.framework.models,treeJson)
            modelEvent(config)
            config.framework.fitModelToViewport()
            config.event.loading('afterLoading',treeJson)
        },
        renderPointToLines: (values) => {
            config.lineData = values || config.lineData
            if (!config.lineData) return
            let linesPos = config.lineData.map(item => {
                let position = config.pointAttrMethods.getWorldPosition(item.index);
                position.z = 0;
                return [
                    position,
                    getScreenToWorld.bind(config)(item.mouse.x,item.mouse.y)
                ]
            })
            // updateLines方法参数案例
            // [
            //     [
            //         { x: 34, y: 43, z: 0 },
            //         { x: 50, y: 50, z: 0 },
            //     ]
            // ]
            config.lineAttrMethods.updateLines(linesPos)
        },
        handleEventListener: createSelectEvent(config)
    });

    console.log(config)

    
}

